package com.highveldsky.gwtdatabinding.client.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeListenerProxy;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.highveldsky.gwtdatabinding.client.controller.CarUpdateListener;
import com.highveldsky.gwtdatabinding.shared.model.Car;

public class CarEdit extends Composite {

	private Car car;
	private CarUpdateListener controller;
	private TextBox make;
	private TextBox model;
	private TextBox mileage;
	private TextBox year;
	private TextBox price;
	private FlexTable grid;
	private Button save;
	private Button cancel;
	private FlowPanel buttons;
	private FlowPanel container;
	private PropertyChangeListener[] listeners = new PropertyChangeListener[5];

	public CarEdit(final CarUpdateListener controller, final Car car) {
		super();
		this.controller = controller;
		this.car = car;
		make = new TextBox();
		model = new TextBox();
		mileage = new TextBox();
		year = new TextBox();
		price = new TextBox();
		grid = new FlexTable();
		grid.setWidget(0, 0, new Label("make"));
		grid.setWidget(0, 1, make);
		grid.setWidget(1, 0, new Label("model"));
		grid.setWidget(1, 1, model);
		grid.setWidget(2, 0, new Label("mileage"));
		grid.setWidget(2, 1, mileage);
		grid.setWidget(3, 0, new Label("year"));
		grid.setWidget(3, 1, year);
		grid.setWidget(4, 0, new Label("price"));
		grid.setWidget(4, 1, price);
		save = new Button("save", new SaveClickHandler());
		cancel = new Button("cancel", new CancelClickHandler());
		buttons = new FlowPanel();
		buttons.add(save);
		buttons.add(cancel);
		container = new FlowPanel();
		container.add(grid);
		container.add(buttons);
		this.initWidget(container);
		listeners[0] = bindProperty("make", make);
		listeners[1] = bindProperty("model", model);
		listeners[2] = bindProperty("mileage", mileage);
		listeners[3] = bindProperty("year", year);
		listeners[4] = bindProperty("price", price);
	}

	private PropertyChangeListener bindProperty(final String propertyName, final TextBox textBox) {
		PropertyChangeListener listener = new PropertyChangeListenerProxy(
				propertyName, 
				new PropertyChangeListener() {
					public void propertyChange(PropertyChangeEvent evt) {
						textBox.setText(evt.getNewValue().toString());
					}
				});
		car.addPropertyChangeListener(listener);
		textBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				car.set(propertyName, textBox.getText());
			}
		});
		return listener;
	}
	
	public void cleanup() {
		for (PropertyChangeListener l : listeners) {
			car.removePropertyChangeListener(l);
		}
	}
	
	private class SaveClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			controller.updateCar(car);
		}
	}
	
	private class CancelClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			// TODO Auto-generated method stub
			
		}
	}

}
